package com.fr.web.core.service;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.StringUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.dav.Env;
import com.fr.privilege.manager.PrivilegeManager;
import com.fr.privilege.providers.NoAuthenticationProvider;
import com.fr.web.cache.WebletFactory;
import com.fr.web.core.ShowWorkBookPolicy.Policy4Form;
import com.fr.web.core.WebUtils;
import com.fr.web.platform.PlatformConstants.Failure;
import com.fr.web.platform.PlatformConstants.Success;
import com.fr.web.platform.PlatformControl;
import com.fr.web.platform.PlatformManager;
import com.fr.web.platform.PlatformNoControl;
import com.fr.web.platform.PlatformPermissionAndAuthorityControl;
import com.fr.web.platform.PlatformPermissionControl;
import com.fr.web.platform.module.Module;
import com.fr.web.platform.module.ModuleManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PlatformService extends NoSessionIDService
{
  private static PlatformService PLATFORM_SERVICE = new PlatformService();

  public static PlatformService getInstance()
  {
    return PLATFORM_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return paramString.startsWith("fr_");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    if ("fr_module".equals(paramString))
      createModule(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_platform_manage".equals(paramString))
      createPlatformManageType(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_platform_manage_all_control".equals(paramString))
      createAllControl(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_platform_manage_list_all".equals(paramString))
      listPlatformManagers(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_platform_manage_all_control_save".equals(paramString))
      updateAllControl(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_platform_manage_permission_control".equals(paramString))
      createPermissionControl(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_platform_manage_permission_control_save".equals(paramString))
      updatePermissionControl(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_platform_manage_no_control".equals(paramString))
      createNoControl(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_platform_manage_no_control_save".equals(paramString))
      updateNoControl(paramHttpServletRequest, paramHttpServletResponse);
    else if (PrivilegeService.getInstance().accept(paramString))
      PrivilegeService.getInstance().process(paramHttpServletRequest, paramHttpServletResponse, paramString);
    else if ("fr_platform".equals(paramString))
      generatePlatform(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_getuserinfo".equals(paramString))
      getUserInfo(paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void getUserInfo(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    JSONObject localJSONObject = new JSONObject();
    String str1 = SessionDealWith.getUserIp(paramHttpServletRequest);
    if ((str1.equals("0:0:0:0:0:0:0:1")) || (str1.equals("127.0.0.1")))
      str1 = "localhost";
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "fr_username");
    boolean bool = !(FRContext.getPrivilegeManager().getAuthenticationProvider() instanceof NoAuthenticationProvider);
    localJSONObject.put("username", str2);
    localJSONObject.put("userip", str1);
    localJSONObject.put("authSet", bool);
    localPrintWriter.print(localJSONObject);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void generatePlatform(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    Map localMap = WebUtils.createTemplateMap(paramHttpServletRequest);
    TemplateUtils.dealWithTemplate("/com/fr/web/platform/html/frplatform.html", paramHttpServletResponse, localMap);
  }

  private static void createModule(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    Map localMap = WebUtils.createTemplateMap(paramHttpServletRequest);
    int i = Integer.parseInt(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "index"));
    Module localModule = FRContext.getModuleManager().getModule(i);
    localMap.put("src", TemplateUtils.render(localModule.getSrc(), localMap));
    TemplateUtils.dealWithTemplate("/com/fr/web/platform/html/template.html", paramHttpServletResponse, localMap);
  }

  private static void createPlatformManageType(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    HashMap localHashMap = new HashMap();
    localHashMap.put("rType", FRContext.getPlatformManager().getPlatformControl().getID());
    ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/web/platform/cpt/platformManager.cpt", new ShowWorkBookPolicy.Policy4Form(), localHashMap));
  }

  private static void createAllControl(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/web/platform/cpt/platformRole.cpt", new ShowWorkBookPolicy.Policy4Form()));
  }

  private static void listPlatformManagers(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    PlatformManager localPlatformManager = FRContext.getPlatformManager();
    PlatformControl localPlatformControl = localPlatformManager.getPlatformControl();
    JSONObject localJSONObject = new JSONObject();
    int i = 0;
    int j = localPlatformControl.getAuthoryryNameCount();
    while (i < j)
    {
      String str = localPlatformControl.getAuthorityName(i);
      localJSONObject.put(str, str);
      ++i;
    }
    localPrintWriter.print(localJSONObject);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void updateAllControl(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str1 = PlatformConstants.Success.DATA_SAVE_INFO;
    try
    {
      String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "__parameters__");
      JSONArray localJSONArray = new JSONArray(str2);
      PlatformManager localPlatformManager = FRContext.getPlatformManager();
      PlatformPermissionAndAuthorityControl localPlatformPermissionAndAuthorityControl = new PlatformPermissionAndAuthorityControl();
      localPlatformManager.setPlatformControl(localPlatformPermissionAndAuthorityControl);
      int i = 0;
      int j = localJSONArray.length();
      while (i < j)
      {
        localPlatformPermissionAndAuthorityControl.addAuthorityName(localJSONArray.getJSONObject(i).getString("text"));
        ++i;
      }
      Env localEnv = FRContext.getCurrentEnv();
      try
      {
        localEnv.writeResource(localPlatformManager);
      }
      catch (IOException localIOException)
      {
        FRContext.getLogger().log(Level.WARNING, localIOException.getMessage(), localIOException);
      }
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      str1 = PlatformConstants.Failure.DATA_SAVE_INFO;
    }
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.print(str1);
    localPrintWriter.flush();
  }

  private static void createPermissionControl(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/web/platform/cpt/platformPermissionControl.cpt", new ShowWorkBookPolicy.Policy4Form()));
  }

  private static void updatePermissionControl(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = PlatformConstants.Success.DATA_SAVE_INFO;
    PlatformManager localPlatformManager = FRContext.getPlatformManager();
    PlatformPermissionControl localPlatformPermissionControl = new PlatformPermissionControl();
    localPlatformManager.setPlatformControl(localPlatformPermissionControl);
    Env localEnv = FRContext.getCurrentEnv();
    try
    {
      localEnv.writeResource(localPlatformManager);
    }
    catch (IOException localIOException)
    {
      FRContext.getLogger().log(Level.WARNING, localIOException.getMessage(), localIOException);
      str = PlatformConstants.Failure.DATA_SAVE_INFO;
    }
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.print(str);
    localPrintWriter.flush();
  }

  private static void createNoControl(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/web/platform/cpt/platformNoControl.cpt", new ShowWorkBookPolicy.Policy4Form()));
  }

  private static void updateNoControl(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = PlatformConstants.Success.DATA_SAVE_INFO;
    PlatformManager localPlatformManager = FRContext.getPlatformManager();
    PlatformNoControl localPlatformNoControl = new PlatformNoControl();
    localPlatformManager.setPlatformControl(localPlatformNoControl);
    Env localEnv = FRContext.getCurrentEnv();
    try
    {
      localEnv.writeResource(localPlatformManager);
    }
    catch (IOException localIOException)
    {
      FRContext.getLogger().log(Level.WARNING, localIOException.getMessage(), localIOException);
      str = PlatformConstants.Failure.DATA_SAVE_INFO;
    }
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.print(str);
    localPrintWriter.flush();
  }

  private static String createPlatformLogoutURL(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "fr_username");
    if (StringUtils.isBlank(str))
      return "";
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("<li class='ui-state-default ui-corner-top' style='float:right'>");
    localStringBuffer.append("<a href='");
    localStringBuffer.append(TemplateUtils.render("${servletURL}?op=auth_logout", WebUtils.createTemplateMap(paramHttpServletRequest)));
    localStringBuffer.append("'>");
    localStringBuffer.append(Inter.getLocText("Username"));
    localStringBuffer.append(":");
    localStringBuffer.append(str);
    localStringBuffer.append("&nbsp;&nbsp;");
    localStringBuffer.append(Inter.getLocText("Logout"));
    localStringBuffer.append("</a></li>");
    return localStringBuffer.toString();
  }
}